package com.tl.sun.ui.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.flyco.tablayout.SlidingTabLayout;
import com.tl.sun.R;
import com.tl.sun.widgets.CustomViewPager;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 作者：HQY on 2018/11/20 13:47
 * 邮箱：hqy_xz@126.com
 */

public class ComboFragment extends Fragment {
    @Bind(R.id.rl_generic_back)
    RelativeLayout mRlGenericBack;
    @Bind(R.id.tv_generic_title)
    TextView mTvGenericTitle;
    @Bind(R.id.tl_combo_top)
    SlidingTabLayout mTlComboTop;
    @Bind(R.id.vp_combo_bottom)
    CustomViewPager mVpComboBottom;
    private View rootView;
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private String mTitles[];
    private MyPagerAdapter mAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_combo, container, false);
            ButterKnife.bind(this, rootView);
            init();
        }
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }
        return rootView;
    }

    private void init() {
        mTvGenericTitle.setText("套餐购买");
        mRlGenericBack.setVisibility(View.GONE);
        mFragments.add(PayFragment.newInstance("1"));
        mFragments.add(PayFragment.newInstance("2"));
        mTitles = getActivity().getResources().getStringArray(R.array.combo_text);
        mAdapter = new MyPagerAdapter(getActivity().getSupportFragmentManager());
        mVpComboBottom.setAdapter(mAdapter);
        mTlComboTop.setViewPager(mVpComboBottom);
        mVpComboBottom.setCurrentItem(0);
    }

    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }
    }


}
